﻿using System;
using System.Collections.Generic;
using System.Linq;
using WebApiV.Data.Entity;
using WebApiV.Domain;
using WebApiV.Domain.Enum;
using WebApiV.Interface.Log;

namespace WebApiV.Implement.Log
{
    public class Log : ILog
    {
        private readonly IRespository<Logs> _LogRespository;

        public Log(IRespository<Logs> LogRespository)
        {
            _LogRespository = LogRespository;
        }

        public void Clear()
        {
            var list = _LogRespository.Table.ToList();
            _LogRespository.Delete(list);
        }

        public void Delete(Logs log)
        {
            _LogRespository.Delete(log);
        }

        public Logs GetLogById(int id)
        {
            return _LogRespository.GetById(id);
        }

        public void Insert(Logs log)
        {
            _LogRespository.Insert(log);
        }

        public IEnumerable<Logs> Query(DateTime beginTime, DateTime endTime, LogsLevel logLevel, int userId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;

            var list = _LogRespository.Table.Where(x => x.CreateTime >= bTime && x.CreateTime <= eTime && x.LogsLevel == (int)logLevel);

            if (userId != 0)
            {
                list = list.Where(x => x.UserId == userId);
            }
            return list;
        }
    }
}
